package com.init.bootframe.system.controller;

import com.init.bootframe.system.service.IUserService;
import com.init.bootframe.system.service.IUploadFileService;
import com.init.bootframe.system.vo.UserVO;
import com.init.bootframe.util.TmompConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 用户controller
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(UserController.class); // 日志

    @Autowired
    private IUserService userService; //用户service

    @Autowired
    private IUploadFileService uploadFileService; //文件上传service

    /**
     * 登录
     * @param userVO
     * @return
     * @throws Exception
     */
    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody UserVO userVO) throws Exception {
        Map<String, Object> result = userService.login(userVO);
        return result;
    }

    /**
     * 退出登录
     * @return
     * @throws Exception
     */
    @PostMapping("/quit")
    public Map<String, Object> quit() throws Exception {
        Map<String, Object> result = userService.quit();
        return result;
    }


    /**
     * 获取当前登录的用户信息
     * @return
     * @throws Exception
     */
    @PostMapping("/findLoginUser")
    public UserVO findLoginUser() throws Exception {
        UserVO user = userService.findLoginUser();
        return user;
    }

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Map<String, Object> upload(@RequestParam("upload_file") MultipartFile file) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (null != file) {
                System.out.println("fileName:"+ file.getName());
                resultMap = uploadFileService.uploadFile(file, "imgFile");
            }else{
                resultMap.put("result",Boolean.FALSE);
                resultMap.put(TmompConstant.BaseDataConstant.DEFAULT_VALUE_MSG, "上传文件不能为空");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("文件上传异常！！！！");
            resultMap.put("result",Boolean.FALSE);
            throw new RuntimeException(e);
        }
        return resultMap;
    }

    /**
     * 分页 list查询
     * @param userVO
     * @return
     * @throws Exception
     */
    @PostMapping("/findUserList")
    public Map<String, Object> findUserList(@RequestBody UserVO userVO) throws Exception {
        Map<String, Object> result = userService.findUserList(userVO);
        return result;
    }

    /**
     * list 查询
     * @return
     * @throws Exception
     */
    @PostMapping("/findList")
    public List<UserVO> findList() throws Exception {
        List<UserVO> result = userService.findList();
        return result;
    }

    /**
     * 详情查询
     * @param userVO
     * @return
     * @throws Exception
     */
    @PostMapping("/findUser")
    public UserVO findUser(@RequestBody UserVO userVO) throws Exception {
        UserVO user = userService.findUser(userVO);
        return user;
    }

    /**
     * 添加用户
     * @param userVO
     * @return
     * @throws Exception
     */
    @PostMapping("/addUser")
    public Map<String, Object> addUser(@RequestBody UserVO userVO) throws Exception {
        Map<String, Object> result = userService.addUser(userVO);
        return result;
    }

    /**
     * 账号唯一判断
     * @param userVO
     * @return
     * @throws Exception
     */
    @PostMapping("/judgeAccount")
    public Map<String, Object> judgeAccount(@RequestBody UserVO userVO) throws Exception {
        Map<String, Object> result = userService.judgeAccount(userVO);
        return result;
    }

    /**
     * 更新用户信息
     * @param userVO
     * @return
     * @throws Exception
     */
    @PostMapping("/updateUser")
    public Map<String, Object> updateUser(@RequestBody UserVO userVO) throws Exception {
        Map<String, Object> result = userService.updateUser(userVO);
        return result;
    }

    /**
     * 删除用户
     * @param userVO
     * @return
     * @throws Exception
     */
    @PostMapping("/deleteUser")
    public Map<String, Object> deleteUser(@RequestBody UserVO userVO) throws Exception {
        Map<String, Object> result = userService.deleteUser(userVO);
        return result;
    }

    /**
     * 更新密码
     * @param userVO
     * @return
     * @throws Exception
     */
    @PostMapping("/updatePassword")
    public Map<String, Object> updatePassword(@RequestBody UserVO userVO) throws Exception {
        Map<String, Object> result = userService.updatePassword(userVO);
        return result;
    }


}
